/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddNewBilling.java
 *
 * Created on 21-04-2012, 16:27:17
 */
package GUI;

import entities.BillingDetails;
import entities.PatientDetails;

import java.util.ArrayList;

import javax.swing.JOptionPane;
import model.Bill.TableModelPatient;
import utilities.BillManage;

import utilities.PatientManage;

/**
 *
 * @author Dell
 */
public class LoadPatient extends javax.swing.JDialog {

    private ArrayList<PatientDetails> patientList, patientList1;
    private PatientManage patientManage;
    private MainFunction mf;
    private BillManage bm;
    private ArrayList<BillingDetails> billList;

    /** Creates new form AddNewBilling */
    public LoadPatient(java.awt.Frame parent, boolean modal, ArrayList<PatientDetails> patientList, PatientManage patientManage,BillManage billManage,ArrayList<BillingDetails> billList) {
        super(parent, modal);
        initComponents();
        setModelPatient();
        // JOptionPane.showMessageDialog(rootPane, patientList.size());
        this.patientList = new ArrayList<PatientDetails>();
        this.patientList = patientList;
        this.billList=new ArrayList<BillingDetails>();
        this.billList=billList;
        this.patientManage = new PatientManage(patientList, mf);
        this.patientManage = patientManage;
        this.bm=new BillManage(null, null, mf);
        this.bm=billManage;

    }

    private void setModelPatient() {
        String[] header = {"Patient Name", "Address", "Date Of Birth", "Gender"};
        TableModelPatient tmp = new TableModelPatient(patientList1);
        tmp.setHeader(header);
        tblPatient.setModel(tmp);
    }

    private void findPatient() {
        patientList1 = new ArrayList<PatientDetails>();
        boolean flag = true;
        for (int k = 0; k < this.patientList.size(); k++) {
            String name = this.patientList.get(k).getpFName() + " " + this.patientList.get(k).getpLName();
            int i = 0, j = 0, m, n;
            n = name.length() - 1;
            m = txtName.getText().length() - 1;
            do {
                if (name.charAt(i) == txtName.getText().charAt(j)) {
                    i++;
                    j++;

                } else {
                    i = i - j + 1;
                    j = 0;

                }

            } while (j <= m && i <= n);
            if (j > m) {

                // JOptionPane.showMessageDialog(rootPane, this.patientList.get(k).getpFName());

                flag = false;

                patientList1.add(this.patientList.get(k));
                //     return this.patientList.get(k);

            }

        }

        if (flag) {
            JOptionPane.showMessageDialog(rootPane, "Not Exist!!!");
        }
        setModelPatient();
        //  return null;

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblPatient = new javax.swing.JTable();
        btSubmit = new javax.swing.JButton();
        btRegister = new javax.swing.JButton();
        btSearch = new javax.swing.JButton();
        btCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Patient ");

        jLabel2.setText("Patient Name");

        txtName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNameActionPerformed(evt);
            }
        });

        tblPatient.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblPatient);

        btSubmit.setText("Submit");
        btSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSubmitActionPerformed(evt);
            }
        });

        btRegister.setText("Register");
        btRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRegisterActionPerformed(evt);
            }
        });

        btSearch.setText("Search");
        btSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSearchActionPerformed(evt);
            }
        });

        btCancel.setText("Cancel");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btSearch))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(148, 148, 148)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 382, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(88, 88, 88)
                        .addComponent(btSubmit)
                        .addGap(18, 18, 18)
                        .addComponent(btRegister)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btCancel)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btSearch))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btSubmit)
                    .addComponent(btRegister)
                    .addComponent(btCancel))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRegisterActionPerformed
        this.setVisible(false);
        AddNewPatient anp = new AddNewPatient(null, true, this.patientManage,bm,billList);
        anp.setVisible(true);
    }//GEN-LAST:event_btRegisterActionPerformed

    private void txtNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNameActionPerformed
    }//GEN-LAST:event_txtNameActionPerformed

    private void btSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSearchActionPerformed


        findPatient();

    }//GEN-LAST:event_btSearchActionPerformed

    private void btSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSubmitActionPerformed
        int i = tblPatient.getSelectedRow();
        if(i>=0){
                    this.dispose();

        AddNewBill anb = new AddNewBill(mf, rootPaneCheckingEnabled, bm, mf, patientList1.get(i),billList);

        anb.setVisible(true);
        }


        //   addNewBill.setVisible(true);
    }//GEN-LAST:event_btSubmitActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                LoadPatient dialog = new LoadPatient(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancel;
    private javax.swing.JButton btRegister;
    private javax.swing.JButton btSearch;
    private javax.swing.JButton btSubmit;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblPatient;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables
}
